package com.lyn.watches;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.Typeface;
import android.graphics.drawable.shapes.Shape;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 复古表盘View
 */
public class WatchesView extends View {
    private Paint paint;
    private Context context;
    //心率
    private Bitmap heartRate_bitmap;
    private volatile String heartRate="120MP";
    //心
    private Bitmap heart_bitmap;
    private volatile String heart="68MM";
    //体温
    private Bitmap body_temperature_bitmap;
    private volatile String bodyTemperature="36.4°C";
    //血压
    private Bitmap blood_pressure_bitmap;
    private volatile String bloodPressure="120MG";

    private Bitmap rain_day_Bitmap;
    private Bitmap snow_day_Bitmap;
    private Bitmap suny_day_Bitmap;
    private Bitmap weather_no_data_Bitmap;
    private Bitmap battery_Bitmap;

    //下雨天
    public static final int RAINY_DAY = 0;
    //下雪天
    public static final int SNOWING_DAY = 1;
    //晴天
    public static final int SUNNY_DAY = 2;
    //没有数据
    public static final int NO_WEATHER_DATA = 3;
    //天气类型
    private volatile int weatherType;
    //天气气温
    private volatile String weatherTemperature="--°C";
    //时
    private volatile int timeAngle;
    //分
    private volatile int minuteAngle;
    //秒
    private volatile int secondAngle;
    //星期
    private volatile String week="星期四";
    //日月
    private volatile String sunAndMoon="01月1日";

    private Date date;

    private SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss");

    public WatchesView(Context context) {
        super(context);
        this.context = context;
        init();
    }

    public WatchesView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    /**
     * 设置心率
     * @param heartRate
     */
    public void setHeartRate(String heartRate) {
        this.heartRate = heartRate;
    }

    /**
     * 设置心
     * @param heart
     */
    public void setHeart(String heart) {
        this.heart = heart;
    }

    /**
     * 设置体温
     * @param bodyTemperature
     */
    public void setBodyTemperature(String bodyTemperature) {
        this.bodyTemperature = bodyTemperature;
    }

    /**
     * 设置血压
     * @param bloodPressure
     */
    public void setBloodPressure(String bloodPressure) {
        this.bloodPressure = bloodPressure;
    }

    /**
     * 设置天气类型
     * @param weatherType
     */
    public void setWeatherType(int weatherType) {
        this.weatherType = weatherType;
    }

    /**
     * 设置天气温度
     * @param weatherTemperature
     */
    public void setWeatherTemperature(String weatherTemperature) {
        this.weatherTemperature = weatherTemperature;
    }

    /**
     * 设置星期
     * @param week
     */
    public void setWeek(String week) {
        this.week = week;
    }

    /**
     *设置月日
     * @param sunAndMoon
     */
    public void setSunAndMoon(String sunAndMoon) {
        this.sunAndMoon = sunAndMoon;
    }

    private void init() {
        date = new Date();

        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(2);
        paint.setTextSize(18);
        paint.setStyle(Paint.Style.STROKE);
        paint.setTextAlign(Paint.Align.CENTER);
        Typeface typeface = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
        paint.setTypeface(typeface);


        heartRate_bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.heart_rate);
        heart_bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.heart);
        body_temperature_bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.body_temperature);
        blood_pressure_bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.blood_pressure);

        rain_day_Bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.rain_day, null);
        snow_day_Bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.snow_day, null);
        suny_day_Bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.suny_day, null);
        weather_no_data_Bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.weather_no_data, null);
        battery_Bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_gundam_battery, null);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, widthMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        invalidate();
        date.setTime(System.currentTimeMillis());

        String format = this.format.format(date);
        String[] split = format.split(":");

        secondAngle = Integer.parseInt(split[2]);
        secondAngle = 360 / 60 * secondAngle - 90;
        minuteAngle = Integer.parseInt(split[1]);
        minuteAngle = 360 / 60 * minuteAngle - 90;
        timeAngle = Integer.parseInt(split[0]);
        timeAngle = (360 / 12 * timeAngle - 90) + 360 / 12 / 60;


        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(4);
        int width = canvas.getWidth();
        int height = canvas.getHeight();

        int centerX = width / 2;
        int centerY = height / 2;

        canvas.drawRGB(45, 74, 106);

        //外圆半径
        int r = centerX - 30;
        //刻度条内圆半径
        int r2 = r - 8;

        paint.setColor(Color.WHITE);
        canvas.drawCircle(centerX, centerY, r, paint);

        for (int i = 0; i < 60; i++) {
            int angle = i * 6;
            boolean isCrude = false;
            if (angle == 0
                    || angle == 30
                    || angle == 60
                    || angle == 90
                    || angle == 120
                    || angle == 150
                    || angle == 180
                    || angle == 210
                    || angle == 240
                    || angle == 270
                    || angle == 300
                    || angle == 330) {
                isCrude = true;
            }
            int x = (int) (r * Math.cos(angle * Math.PI / 180) + centerX);
            int y = (int) (r * Math.sin(angle * Math.PI / 180) + centerY);

            int x2 = (int) ((isCrude ? r2 - 5 : r2) * Math.cos(angle * Math.PI / 180) + centerX);
            int y2 = (int) ((isCrude ? r2 - 5 : r2) * Math.sin(angle * Math.PI / 180) + centerY);

            int x3 = (int) ((isCrude ? r2 - 16 : r2) * Math.cos(angle * Math.PI / 180) + centerX);
            int y3 = (int) ((isCrude ? r2 - 16 : r2) * Math.sin(angle * Math.PI / 180) + centerY);

            paint.setStyle(Paint.Style.FILL);
            if (angle == 0) {
                canvas.drawText(3 + "", x3, baseLine(y3), paint);
            }
            if (angle == 90) {
                canvas.drawText(6 + "", x3, baseLine(y3), paint);
            }
            if (angle == 180) {
                canvas.drawText(9 + "", x3, baseLine(y3), paint);
            }
            if (angle == 270) {
                canvas.drawText(12 + "", x3, baseLine(y3), paint);
            }

            if (isCrude) {
                paint.setStrokeWidth(4.5f);
            } else {
                paint.setStrokeWidth(2.5f);
            }
            canvas.drawLine(x, y, x2, y2, paint);
        }

        //最内圆半径
        int r3 = r / 6 * 4;
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(context.getResources().getColor(R.color.ringColor));
        canvas.drawCircle(centerX, centerY, r3, paint);

        //画天气
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.WHITE);
        Rect weatherRect = new Rect();
        weatherRect.left = centerX - 40;
        weatherRect.top = centerY - 48;
        weatherRect.right = centerX + 40;
        weatherRect.bottom = centerY - 30;
        switch (weatherType) {
            case RAINY_DAY:
                canvas.drawBitmap(rain_day_Bitmap, null, new Rect(weatherRect.left
                        , weatherRect.top
                        , weatherRect.left + 30
                        , weatherRect.top + 30), paint);
                break;
            case SNOWING_DAY:
                canvas.drawBitmap(snow_day_Bitmap, null, new Rect(weatherRect.left
                        , weatherRect.top
                        , weatherRect.left + 30
                        , weatherRect.top + 30), paint);
                break;
            case SUNNY_DAY:
                canvas.drawBitmap(suny_day_Bitmap, null, new Rect(weatherRect.left
                        , weatherRect.top
                        , weatherRect.left + 30
                        , weatherRect.top + 30), paint);
                break;
            default:
                canvas.drawBitmap(weather_no_data_Bitmap, null, new Rect(weatherRect.left
                        , weatherRect.top
                        , weatherRect.left + 30
                        , weatherRect.top + 30), paint);
                break;
        }

        canvas.drawText(weatherTemperature,centerX+(weatherRect.right-weatherRect.left)/4
                ,baseLine((weatherRect.top + 30-weatherRect.top)/2)+weatherRect.top,paint);

        //画日期
        Rect timeRect=new Rect();
        timeRect.left=centerX-40;
        timeRect.top=centerY+25;
        timeRect.right=centerX+40;
        timeRect.bottom=centerY+50;

        canvas.drawText(week,centerX,baseLine( timeRect.top),paint);

        canvas.drawText(sunAndMoon,centerX,baseLine(timeRect.bottom),paint);

        //时针
        int TimeX = (int) ((r3 / 6 * 4) * Math.cos(timeAngle * Math.PI / 180) + centerX);
        int TimeY = (int) ((r3 / 6 * 4) * Math.sin(timeAngle * Math.PI / 180) + centerY);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.WHITE);
        paint.setStrokeWidth(7);
        canvas.drawLine(centerX, centerY, TimeX, TimeY, paint);
        canvas.drawCircle(TimeX, TimeY, 3.5f, paint);

        //分针
        int MinuteX = (int) ((r3 / 6 * 5) * Math.cos(minuteAngle * Math.PI / 180) + centerX);
        int MinuteY = (int) ((r3 / 6 * 5) * Math.sin(minuteAngle * Math.PI / 180) + centerY);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.WHITE);
        paint.setStrokeWidth(7);
        canvas.drawLine(centerX, centerY, MinuteX, MinuteY, paint);
        canvas.drawCircle(MinuteX, MinuteY, 3.5f, paint);

        //秒针
        int secondX = (int) ((r3 - 3) * Math.cos(secondAngle * Math.PI / 180) + centerX);
        int secondY = (int) ((r3 - 3) * Math.sin(secondAngle * Math.PI / 180) + centerY);
        paint.setColor(Color.RED);
        paint.setStrokeWidth(2);
        canvas.drawLine(centerX, centerY, secondX, secondY, paint);

        //指针中心白小圆
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.WHITE);
        canvas.drawCircle(centerX, centerY, 10, paint);

        //指针中心红点
        paint.setColor(Color.RED);
        canvas.drawCircle(centerX, centerY, 2, paint);

        int padding = 30;
        Rect rect = new Rect();
        rect.left = padding;
        rect.top = padding;
        rect.right = canvas.getWidth() - padding;
        rect.bottom = canvas.getHeight() - padding;

        Rect heartRateRect = new Rect();
        heartRateRect.left = rect.left;
        heartRateRect.top = rect.top;
        heartRateRect.right = rect.left + 38;
        heartRateRect.bottom = rect.top + 38;
        canvas.drawBitmap(heartRate_bitmap, null, heartRateRect, paint);
        paint.setColor(Color.WHITE);
        canvas.drawText(heartRate, (heartRateRect.right - heartRateRect.left) / 2 + heartRateRect.left
                , baseLine(heartRateRect.top - 3), paint);

        Rect heartRect = new Rect();
        heartRect.left = rect.right - 33;
        heartRect.top = rect.top;
        heartRect.right = rect.right;
        heartRect.bottom = rect.top + 33;
        canvas.drawBitmap(heart_bitmap, null, heartRect, paint);
        canvas.drawText(heart, (heartRect.right - heartRect.left) / 2 + heartRect.left
                , baseLine(heartRect.top - 3), paint);

        Rect body_temperatureRect = new Rect();
        body_temperatureRect.left = rect.left;
        body_temperatureRect.top = rect.bottom - 33;
        body_temperatureRect.right = rect.left + 33;
        body_temperatureRect.bottom = rect.bottom;
        canvas.drawBitmap(body_temperature_bitmap, null, body_temperatureRect, paint);
        canvas.drawText(bodyTemperature, (body_temperatureRect.right - body_temperatureRect.left) / 2 + body_temperatureRect.left
                , baseLine(body_temperatureRect.bottom + 8), paint);

        Rect blood_pressureRect = new Rect();
        blood_pressureRect.left = rect.right - 33;
        blood_pressureRect.top = rect.bottom - 33;
        blood_pressureRect.right = rect.right;
        blood_pressureRect.bottom = rect.bottom;
        canvas.drawBitmap(blood_pressure_bitmap, null, blood_pressureRect, paint);
        canvas.drawText(bloodPressure, (blood_pressureRect.right - blood_pressureRect.left) / 2 + blood_pressureRect.left
                , baseLine(blood_pressureRect.bottom + 8), paint);


    }

    private float baseLine(int y) {
        //计算baseline
        Paint.FontMetrics fontMetrics = paint.getFontMetrics();
        float distance = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom;
        float baseline = y + distance;
        return baseline;
    }
}
